package io.renren.sf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.swaggerbootstrapui.util.CommonUtils;
import io.renren.common.utils.CommonUtil;
import io.renren.common.utils.ResponseData;
import io.renren.sf.entity.CampusEntity;
import io.renren.sf.entity.HzFileEntity;
import io.renren.sf.service.HzCityService;
import io.renren.sf.service.HzFileService;
import io.renren.sf.service.HzTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("publicResource")
@Api(tags = "公共资源管理")
@ApiIgnore
public class PublicController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    HzFileService hzFileService;

    @Autowired
    HzCityService hzCityService;

    @Autowired
    HzTeacherService hzTeacherService;

    @ApiOperation("根据条件查找当前目录下的文件")
    @GetMapping("/getPublicFile")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页pageIndex", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "limit", value = "每页数量pageSize", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "upperFid", value = "上层文件目录id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pcatype", value = "0：省1：市2：区/县", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pcaid", value = "省市区/县id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名", paramType = "query", dataType = "String", required = true),
    })
    public ResponseData<Page<HzFileEntity>> getPublicFile(@RequestParam Map<String, Object> params) {
        Page<HzFileEntity> page = hzFileService.getPublicFile(params);
        return ResponseData.success(page);
    }


    @ApiOperation("后台创建目录/上传公共资源")
    @PostMapping("/uploadFileToPublic")
    public ResponseData uploadFileToPublic(@RequestBody HzFileEntity fileEntity) {
        // 后台全是公共 后台直接审核通过
        fileEntity.setFileBelongto(0).setVerifyState(1).setCreateTime(new Date()).setUpdateTime(new Date());
        // 若为文件夹需要更改文件大小的格式
        if (fileEntity.getFileType() == 1)
            fileEntity.setFileSize(CommonUtil.getFileSizeConvert(Long.valueOf(fileEntity.getFileSize())));
        boolean flag = hzFileService.save(fileEntity);
        return ResponseData.success(flag ? "创建成功" : "创建失败", flag);
    }

    @ApiOperation("后台删除文件/目录(包含单个和多个)")
    @PostMapping("/deleteFile")
    public ResponseData deleteFile(@RequestBody List<Long> ids) {
        boolean flag = hzFileService.deleteFile(ids);
        return ResponseData.success(flag ? "删除成功" : "删除失败", flag);
    }

    @ApiOperation("后台修改文件/目录信息")
    @PostMapping("/updateFile")
    public ResponseData updateFile(@RequestBody HzFileEntity fileEntity) {
        fileEntity.setUpdateTime(new Date());
        boolean flag = hzFileService.updateById(fileEntity);
        return ResponseData.success(flag ? "修改成功" : "修改失败", flag);
    }

    @ApiOperation("审核文件")
    @PostMapping("/approvalFile")
    public ResponseData approvalFile(@RequestBody HzFileEntity fileEntity) {
        fileEntity.setUpdateTime(new Date());
        if(fileEntity.getUpperFid() == null) {
            fileEntity.setUpperFid(0L);
        }
        boolean flag = hzFileService.updateById(fileEntity);
        return ResponseData.success(flag ? "提交成功" : "提交失败", flag);
    }

    @ApiOperation("根据条件查找不同状态审核文件")
    @GetMapping("/getApprovalFile")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页pageIndex", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "limit", value = "每页数量pageSize", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "verifyState", value = "审核状态0.待审1.通过2.不通过", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pcatype", value = "0：省1：市2：区/县", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pcaid", value = "省市区/县id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名", paramType = "query", dataType = "String", required = true),
    })
    public ResponseData<Page<HzFileEntity>> getApprovalFile(@RequestParam Map<String, Object> params) {
        Page<HzFileEntity> page = hzFileService.getApprovalFile(params);
        return ResponseData.success(page);
    }

    /*@ApiOperation("获取目录")
    @GetMapping("/getDirectory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "upperFid", value = "上层文件目录id,0为根目录", paramType = "query", dataType = "String", required = true),
    })
    public ResponseData<List<HzFileEntity>> getDirectory(@RequestParam("upperFid") String upperFid) {
        return ResponseData.success(hzFileService.list(new QueryWrapper<HzFileEntity>().eq("upper_fid", Long.parseLong(upperFid)).eq("file_belongto", 0).eq("file_type", 0).and((wrapper -> wrapper.eq("verify_state", 1).or().isNull("verify_state")))));
    }*/

    @ApiOperation("获取当前目录下所有文件夹，除去自身文件夹")
    @GetMapping("/getDirectory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "upperFid", value = "上层文件目录id,0为根目录", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "mFid", value = "待移动文件id", paramType = "query", dataType = "String", required = true),
    })
    public ResponseData<List<HzFileEntity>> getDirectory(@RequestParam("upperFid") String upperFid, @RequestParam("mFid") String mFid) {
        return ResponseData.success(hzFileService.list(new QueryWrapper<HzFileEntity>().eq("upper_fid", Long.parseLong(upperFid)).eq("file_belongto", 0).eq("file_type", 0).and((wrapper -> wrapper.eq("verify_state", 1).or().isNull("verify_state"))).ne("id", Long.parseLong(mFid))));
    }
}
